<template>
  <div class="commentfooter">
    <div class="addcomment" v-if="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlefoucs" />
      <span
        class="comment"
        @click="
          $router.push({ path: `/comment/${article.id}` }).catch(() => {})
        "
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ article.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        @click="starArticle"
        :class="{ active: article.has_star }"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea
        ref="commtext"
        rows="5"
        v-model="commentConent"
        v-myfocus
      ></textarea>
      <div>
        <span @click="sendComment">发 送</span>
        <span @click="cancelReplay">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { post_star, sendpost_comment } from "@/apis/post.js";
import { Toast } from "vant";
import { myfocus } from "@/utils/mydirectives.js";
export default {
  props: {
    article: {
      type: Object,
      required: true,
    },
    // 点击父组件的回复传递过来的当层回复数据
    comment: {
      type: Object,
    },
  },
  directives: {
    myfocus,
  },
  data() {
    return {
      isFocus: false,
      commentConent: "",
      commentData: {},
    };
  },
  watch: {
    // 点击父组件的评论就会为comment赋值，有变化就监听从而使评论框打开
    comment() {
      if (this.comment == null) return;
      this.isFocus = true;
    },
    // comment: {
    //   handler() {
    //     this.isFocus = true;
    //   },
    //   deep: true,
    // },
  },
  mounted() {},
  // updated() {
  //   this.$refs.commtext.focus();
  // },
  methods: {
    async starArticle() {
      // console.log(this.article);
      let res = await post_star(this.article.id);
      // console.log("收藏", res);
      Toast.success(res.data.message);
      this.article.has_star = !this.article.has_star;
    },
    handlefoucs() {
      this.isFocus = !this.isFocus;
      // this.$nextTick(() => {
      //   this.$refs.commtext.focus();
      // });
      // setTimeout(() => {
      //   this.$refs.commtext.focus();
      // }, 0);
    },
    cancelReplay() {
      this.isFocus = false;
      // console.log(this.isFocus);
      this.commentConent = "";
      this.$emit("resetComment");
    },
    async sendComment() {
      if (this.commentConent == "") {
        Toast.fail("请输入评论内容！");
        return;
      }
      let commentData = {
        content: this.commentConent,
      };
      if (this.comment) {
        commentData.parent_id = this.comment.id;
      }
      let res = await sendpost_comment(this.article.id, commentData);
      console.log("评论结果", res);
      Toast.success(res.data.message);
      this.commentConent = "";
      this.isFocus = !this.isFocus;
      this.$emit("refershData");
      this.$emit("resetComment");
    },
  },
};
</script>

<style lang='less' scoped>
.commentfooter {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
  span:first-child {
    margin-bottom: 5px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  background-color: #fff;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  .active {
    color: #f00;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>
